<?php

class fop_messageController extends Zend_Controller_Action
{
  function init() {
  	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    $this->getHelper('viewRenderer')->setNoRender();
    require_once('apidefinition/objects/message.php');
  }
  
  function getAction() {
	
	//We get also the last published version of the message
    require_once('fop/objects/message.php');

    try{
      $id = $this->_getParam('id');
      if(empty($id)) $message = new fop_Object_Message($this->_getParam('foplineName'),$this->_getParam('tableName'),'','','','','latest');
      else $message = new fop_Object_Message($this->_getParam('foplineName'),$this->_getParam('tableName'),$this->_getParam('id'));
      $resMessage = $message->asArray();
    } catch (APIDefinition_Object_Exception $e) {
      $data['status'] = 'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();      	
    }
    if(isset($resMessage) && !empty($resMessage)) {
	
      $data['status'] = 'OK';
      $data['message'] = $resMessage;
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;  	
  }
  
  function createAction() {	
  
      require_once('fop/objects/message.php');
	  
    try{
      $message = new fop_Object_Message($this->_getParam('foplineName'),$this->_getParam('tableName'),'','','','','latest');
      $message->update($this->_getParam('message'),$this->_getParam('messageFormat'),$this->_getParam('description'));
    } catch (APIDefinition_Object_Exception $e) {
      $data['status'] = 'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage(); 
      echo serialize($data);
      return;       	
    }
    $data['status'] = 'OK';
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;     
    
  }
  
  function makecurrentAction() {

    require_once('fop/objects/message.php');
	
	try{
      	$message = new fop_Object_Message($this->_getParam('foplineName'),$this->_getParam('tableName'),$this->_getParam('id'));
      	$message->makeCurrent();
      	$resMessage = $message->asArray();
    } catch (APIDefinition_Object_Exception $e) {
      $data['status'] = 'KO';
      $data['error']['num'] = 	$e->getCode();
      $data['error']['message'] = 	$e->getMessage();      	
    }
    if(isset($resMessage) && !empty($resMessage)) {
      $data = $resMessage;	
      $data['status'] = 'OK';
    }
    echo Zend_Registry::get('dt_serializer')->serialize($data);	
    return;     
         
       	
  }
  
  

} //End class
